import cefsharpAdapter from '../cefsharp'

// === 用户相关处理函数 ===
export async function handleUserRequests(method, url, data, params) {
    console.log('🔍 [API] 处理用户请求:', { method, url, data, params })
    
    // POST请求处理
    if (method === 'post') {
        // 用户登录
        if (/\/user\/login$/.test(url)) {
            console.log('🔐 [API] 用户登录')
            return await cefsharpAdapter.callNativeMethod('ValidateUserLogin', data)
        }
        
        // 用户注册
        if (/\/user\/register$/.test(url)) {
            console.log('📝 [API] 用户注册')
            return await cefsharpAdapter.callNativeMethod('RegisterUser', data)
        }
        
        // 用户登出
        if (/\/user\/logout$/.test(url)) {
            console.log('🚪 [API] 用户登出')
            return await cefsharpAdapter.callNativeMethod('LogoutUser', data)
        }
    }
    
    // GET请求处理
    if (method === 'get') {
        // 获取用户信息
        if (/\/user\/info$/.test(url)) {
            console.log('👤 [API] 获取用户信息')
            return await cefsharpAdapter.callNativeMethod('GetUserInfo', params)
        }
        
        // 验证token
        if (/\/user\/verify-token$/.test(url)) {
            console.log('🔍 [API] 验证token')
            return await cefsharpAdapter.callNativeMethod('VerifyUserToken', params)
        }
    }
    
    // PUT请求处理
    if (method === 'put') {
        // 更新用户信息
        if (/\/user\/profile$/.test(url)) {
            console.log('✏️ [API] 更新用户信息')
            return await cefsharpAdapter.callNativeMethod('UpdateUserProfile', data)
        }
        
        // 修改密码
        if (/\/user\/change-password$/.test(url)) {
            console.log('🔑 [API] 修改密码')
            return await cefsharpAdapter.callNativeMethod('ChangeUserPassword', data)
        }
    }
    
    return null
}

// === 用户API封装 ===
export const userAPI = {
    // 用户登录
    login: async(params = {}) => {
        console.log('🔐 [UserAPI] 登录请求:', params)
        try {
            const result = await cefsharpAdapter.callNativeMethod('ValidateUserLogin', params)
            console.log('✅ [UserAPI] 登录成功:', result)
            return result
        } catch (error) {
            console.error('❌ [UserAPI] 登录失败:', error)
            throw error
        }
    },
    
    // 获取用户信息
    getUserInfo: async(params = {}) => {
        console.log('👤 [UserAPI] 获取用户信息:', params)
        try {
            const result = await cefsharpAdapter.callNativeMethod('GetUserInfo', params)
            console.log('✅ [UserAPI] 获取用户信息成功:', result)
            return result
        } catch (error) {
            console.error('❌ [UserAPI] 获取用户信息失败:', error)
            throw error
        }
    },
    
    // 用户注册
    register: async(params = {}) => {
        console.log('📝 [UserAPI] 用户注册:', params)
        try {
            const result = await cefsharpAdapter.callNativeMethod('RegisterUser', params)
            console.log('✅ [UserAPI] 注册成功:', result)
            return result
        } catch (error) {
            console.error('❌ [UserAPI] 注册失败:', error)
            throw error
        }
    },
    
    // 用户登出
    logout: async(params = {}) => {
        console.log('🚪 [UserAPI] 用户登出:', params)
        try {
            const result = await cefsharpAdapter.callNativeMethod('LogoutUser', params)
            console.log('✅ [UserAPI] 登出成功:', result)
            return result
        } catch (error) {
            console.error('❌ [UserAPI] 登出失败:', error)
            throw error
        }
    },
    
    // 验证token
    verifyToken: async(params = {}) => {
        console.log('🔍 [UserAPI] 验证token:', params)
        try {
            const result = await cefsharpAdapter.callNativeMethod('VerifyUserToken', params)
            console.log('✅ [UserAPI] token验证成功:', result)
            return result
        } catch (error) {
            console.error('❌ [UserAPI] token验证失败:', error)
            throw error
        }
    },
    
    // 更新用户信息
    updateProfile: async(params = {}) => {
        console.log('✏️ [UserAPI] 更新用户信息:', params)
        try {
            const result = await cefsharpAdapter.callNativeMethod('UpdateUserProfile', params)
            console.log('✅ [UserAPI] 更新用户信息成功:', result)
            return result
        } catch (error) {
            console.error('❌ [UserAPI] 更新用户信息失败:', error)
            throw error
        }
    },
    
    // 修改密码
    changePassword: async(params = {}) => {
        console.log('🔑 [UserAPI] 修改密码:', params)
        try {
            const result = await cefsharpAdapter.callNativeMethod('ChangeUserPassword', params)
            console.log('✅ [UserAPI] 修改密码成功:', result)
            return result
        } catch (error) {
            console.error('❌ [UserAPI] 修改密码失败:', error)
            throw error
        }
    }
}

export default { handleUserRequests, userAPI } 